define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    if(Config.domain == '' || Config.domain == null){
        Config.domain = 0;
    }
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'virtualmachinehidden/index/?domain=' + Config.domain,
                    add_url: 'virtualmachinehidden/add',
                    edit_url: 'virtualmachinehidden/edit',
                    del_url: 'virtualmachinehidden/del',
                    multi_url: 'virtualmachinehidden/multi',
                    import_url: 'virtualmachinehidden/import',
                    table: 'fedition_account',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'hiddentime',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'number', title: __('Number'), operate: 'LIKE'},
                        // {field: 'number', title: __('Number'), operate: false,
                        //     formatter : function(value, row, index, field){
                        //         return "<span title='" + row.launch + "'>" + value + "</span>";
                        //     },
                        // },
                        {field: 'admin_id', title: __('Admin_id'),visible: false,addClass: "selectpage", extend: "data-source='auth/admin/index' data-field='nickname'"},
                        {field: 'admin_name', title: __('Admin_id'), operate: false},
                        {field: 'manual', title: __('手动申请人'), operate: 'LIKE'},
                        // {field: 'domain', title: __('Domain'),visible: false,addClass: "selectpage", extend: "data-source='dnslist/index' data-field='name'"},
                        // {field: 'domain_name', title: __('Domain'), operate: false},
                        {field: 'ip', title: __('Ip'), operate: 'LIKE'},
                        {field: 'ippassword', title: __('Ippassword'), operate: 'LIKE'},
                        {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'emailpassword', title: __('Emailpassword'), operate: 'LIKE'},
                        {field: 'payment_method', title: __('支付方式'), formatter:Table.api.formatter.flag, operate: 'LIKE',custom:{'自动支付': 'info','手动支付':'warning'}},
                        // {field: 'status', title: __('Status'), searchList: {"normal":__('运营使用中'),"getset":__('初始预备账户'),"notused":__('待分配虚拟机'),"suspend":__('暂停'),"hidden":__('关闭')}, formatter: Table.api.formatter.status,custom: {'notused':'warning', 'normal':'success', 'hidden':'danger', 'suspend':'info','activation':'info','getset':'info'}},
                        {field: 'hiddentime', title: __('账户关闭时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status',title: __('确认关闭'),formatter: Table.api.formatter.toggle,yes: 'confirmclose',no: 'hidden'},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 添加金额类型
            // $(document).on("click", ".btn-moneytype", function () {
            //     var name = $(this).closest(".operate").data("name");
            //     Fast.api.open("feditionmoneytype/add?name=" + name, __('添加金额类型'));
            // });
            $(document).on("click", ".btn-apply", function () {
                var name = $(this).closest(".operate").data("name");
                Fast.api.open("virtualmachineapply/add?name=" + name, __('添加账户申请'));
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            var numbertype = $('#c-numbertype option:first').val();
            add_number(numbertype);
            $("#c-numbertype").change(function(){
                var numbertype = $(this).children('option:selected').val();
                add_number(numbertype);
            });
            function add_number(numbertype) {
                Fast.api.ajax({
                    url: "feditionaccount/add_number",
                    async: false,
                    data: {numbertype: numbertype}
                }, function (data) {
                    $("#c-number").val(data.number);
                    $("#c-numberid").val(data.numberid);
                    return false;
                });
            }

            $(document).on("fa.event.appendfieldlist", "#add-form .btn-append", function (e, obj) {
                Form.events.selectpage(obj);
            });
            Controller.api.bindevent();
            $("#c-account_address").data("formatItem",function (item) {
                return "店铺名称:"+item.name+",店铺域名:"+item.url;
            });
        },
        edit: function () {
            var id = $('#c-id').val();
            $("#c-numbertype").change(function(){
                var numbertype = $(this).children('option:selected').val();
                add_number(numbertype,id);
            });
            function add_number(numbertype,id) {
                Fast.api.ajax({
                    url: "feditionaccount/add_number",
                    async: false,
                    data: {numbertype: numbertype,id: id}
                }, function (data) {
                    $("#c-number").val(data.number);
                    $("#c-numberid").val(data.numberid);
                    return false;
                });
            }

            $(document).on("fa.event.appendfieldlist", "#edit-form .btn-append", function (e, obj) {
                Form.events.selectpage(obj);
            });
            Controller.api.bindevent();
            $("#c-account_address").data("formatItem",function (item) {
                return "店铺名称:"+item.name+",店铺域名:"+item.url;
            });
        },
        api: {
            bindevent: function () {
                require(['selectpage'], function (SelectPage) {
                    var _displayResults = $.fn.selectPage.Constructor.prototype.displayResults;
                    $.fn.selectPage.Constructor.prototype.displayResults = function (self, json, is_query) {
                        _displayResults.call(this, self, json, is_query);
                        if (self.elem.hidden.is("#c-domain")) {
                            $(self.elem.results[0]).append("<p class='text-center' style='margin:5px;'><a class='btn btn-xs btn-info btn-dialog' title='新增对应域名' data-window='top' href='dnslist/add'>新增对应域名</a></p><p style='margin:5px;'>（新增对应域名后需要取消下拉框，重新选择）</p>");
                        }
                        if (self.elem.hidden.is("#c-account_address")) {
                            $(self.elem.results[0]).append("<p class='text-center' style='margin:5px;'><a class='btn btn-xs btn-info btn-dialog' title='新增店铺' data-window='top' href='feditionaccounturl/add'>新增店铺</a></p><p style='margin:5px;'>（新增店铺后需要取消下拉框，重新选择）</p>");
                        }
                    };
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});